Conditions | 20 |
Paths | 337 |
Total Lines | 193 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like DataManager.load often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
201 | DataManager.prototype.load = function (options) { |
||
202 | if ($('#' + this.element.attr('id')).length == 0) { |
||
203 | delete inji.Ui.dataManagers[this.element.attr('id')]; |
||
204 | return; |
||
205 | } |
||
206 | var dataManager = this; |
||
207 | if (typeof this.params == 'string') { |
||
208 | var params = JSON.parse(this.params); |
||
209 | } |
||
210 | if (Object.prototype.toString.call(this.params) === '[object Array]') { |
||
211 | var params = {}; |
||
212 | } else { |
||
213 | var params = this.params; |
||
214 | } |
||
215 | params.limit = this.limit; |
||
216 | params.page = this.page; |
||
217 | params.categoryPath = this.categoryPath; |
||
218 | filters = this.filters; |
||
219 | if (this.element.find('.dataManagerFilters [name^="datamanagerFilters"]').length > 0) { |
||
220 | this.element.find('.dataManagerFilters [name^="datamanagerFilters"]').each(function () { |
||
221 | var maths = $(this).attr('name').match(/\[([^\]]+)\]/g); |
||
222 | for (key in maths) { |
||
223 | maths[key] = maths[key].replace(/([\[\]])/g, ''); |
||
224 | } |
||
225 | if (!filters[maths[0]]) { |
||
226 | filters[maths[0]] = {}; |
||
227 | } |
||
228 | if (typeof maths[2] != 'undefined') { |
||
229 | if (!filters[maths[0]][maths[1]]) { |
||
230 | filters[maths[0]][maths[1]] = {}; |
||
231 | } |
||
232 | filters[maths[0]][maths[1]][maths[2]] = $(this).val(); |
||
233 | } else { |
||
234 | if ($(this).attr('type') == 'checkbox' && !$(this)[0].checked) { |
||
235 | filters[maths[0]][maths[1]] = 0; |
||
236 | } else { |
||
237 | filters[maths[0]][maths[1]] = $(this).val(); |
||
238 | } |
||
239 | } |
||
240 | }); |
||
241 | } |
||
242 | if (this.options.sortable) { |
||
243 | for (var key2 in this.options.cols) { |
||
244 | var colname; |
||
245 | if (typeof this.options.cols[key2] == 'object') { |
||
246 | colname = key2; |
||
247 | } else { |
||
248 | colname = this.options.cols[key2]; |
||
249 | } |
||
250 | if (this.options.sortable.indexOf(colname) == -1) { |
||
251 | continue; |
||
252 | } |
||
253 | var th = $('.' + dataManager.element.attr('id') + '_colname_' + colname.replace(/\:/g, '\\:')); |
||
254 | if (!th.hasClass('sortable')) { |
||
255 | th.html('<a href = "#">' + th.html() + '</a>'); |
||
256 | th.addClass('sortable'); |
||
257 | if (this.options.preSort && this.options.preSort[colname]) { |
||
258 | if (this.options.preSort[colname] == 'asc') { |
||
259 | th.addClass('sorted-asc'); |
||
260 | this.sortered[colname] = 'asc'; |
||
261 | } else if (this.options.preSort[colname] == 'desc') { |
||
262 | th.addClass('sorted-desc'); |
||
263 | this.sortered[colname] = 'desc'; |
||
264 | } |
||
265 | } |
||
266 | //sorted-desc |
||
267 | th.click(function () { |
||
268 | var colname = $(this).data('colname'); |
||
269 | $(this).addClass('clickedsort'); |
||
270 | dataManager.element.find('.sortable').not('.clickedsort').removeClass('sorted-asc').removeClass('sorted-desc'); |
||
271 | $(this).removeClass('clickedsort'); |
||
272 | dataManager.sortered = {}; |
||
273 | if (!$(this).hasClass('sorted-desc') && !$(this).hasClass('sorted-asc')) { |
||
274 | $(this).addClass('sorted-desc'); |
||
275 | dataManager.sortered[colname] = 'desc'; |
||
276 | dataManager.reload(); |
||
277 | } else if ($(this).hasClass('sorted-desc')) { |
||
278 | $(this).removeClass('sorted-desc'); |
||
279 | $(this).addClass('sorted-asc'); |
||
280 | dataManager.sortered[colname] = 'asc'; |
||
281 | dataManager.reload(); |
||
282 | } else if ($(this).hasClass('sorted-asc')) { |
||
283 | $(this).removeClass('sorted-asc'); |
||
284 | delete dataManager.sortered[colname]; |
||
285 | dataManager.reload(); |
||
286 | } |
||
287 | return false; |
||
288 | }) |
||
289 | } |
||
290 | } |
||
291 | } |
||
292 | var data = {params: params, modelName: this.modelName, managerName: this.managerName, filters: filters, sortered: this.sortered, mode: this.mode, all: this.all}; |
||
293 | if (options && options.download) { |
||
294 | data.download = true; |
||
295 | var url = this.ajaxUrl; |
||
296 | if (url.indexOf(inji.options.appRoot) !== 0) { |
||
297 | url = inji.options.appRoot + url; |
||
298 | } |
||
299 | window.location = url + '?' + $.param(data); |
||
300 | return; |
||
301 | } |
||
302 | dataManager.element.find('.datamanagertable tbody').html('<tr><td colspan="' + dataManager.element.find('thead tr th').length + '"><div class = "text-center"><img src = "' + inji.options.appRoot + 'static/moduleAsset/Ui/images/ajax-loader.gif" /></div></td></tr>'); |
||
303 | var instance = this; |
||
304 | |||
305 | |||
306 | var windowHash = {}; |
||
307 | if (window.location.hash && window.location.hash != '#') { |
||
308 | windowHash = JSON.parse(decodeURIComponent(window.location.hash.substr(1))); |
||
309 | } |
||
310 | windowHash[data.modelName + ':' + data.managerName] = data; |
||
311 | window.location.hash = JSON.stringify(windowHash); |
||
312 | inji.Server.request({ |
||
313 | url: this.ajaxUrl, |
||
314 | data: data, |
||
315 | success: function (data) { |
||
316 | dataManager.element.find('.datamanagertable tbody').html(data.rows); |
||
317 | dataManager.element.find('.pagesContainer').html(data.pages); |
||
318 | |||
319 | if (dataManager.options.options && dataManager.options.options.formOnPage) { |
||
320 | $('.' + dataManager.modelName.replace(/\\/g, '_') + '_' + dataManager.managerName + '_create_btn').each(function () { |
||
321 | var createBtn = $(this); |
||
322 | var btnHref = createBtn.attr('href'); |
||
323 | btnHref = btnHref.substr(0, btnHref.indexOf('redirectUrl=')); |
||
324 | btnHref += 'redirectUrl=' + window.location.pathname; |
||
325 | createBtn.attr('href', btnHref + '&dataManagerHash=' + window.location.hash.substr(1)); |
||
326 | }); |
||
327 | } |
||
328 | |||
329 | //dataManager.flowPages(); |
||
330 | if (dataManager.options.sortMode) { |
||
331 | if (dataManager.mode != 'sort') { |
||
332 | dataManager.element.find('.modeBtn').removeClass('active'); |
||
333 | } else { |
||
334 | dataManager.element.find('.modeBtn').addClass('active'); |
||
335 | } |
||
336 | } |
||
337 | $(instance.element).find('.datamanagertable tbody').sortable().sortable("disable"); |
||
338 | if (dataManager.mode == 'sort') { |
||
339 | $(instance.element).find('.datamanagertable tbody').sortable({ |
||
340 | stop: function (event, ui) { |
||
341 | ids = $(instance.element).find('.datamanagertable tbody tr'); |
||
342 | i = 0; |
||
343 | while (ids[i]) { |
||
344 | var key = $($(ids[i++]).find('td').get(1)).text(); |
||
345 | inji.Server.request({ |
||
346 | url: 'ui/dataManager/updateRow', |
||
347 | data: {params: instance.params, modelName: instance.modelName, key: key, col: 'weight', col_value: i, managerName: instance.managerName, silence: true}, |
||
348 | }); |
||
349 | } |
||
350 | } |
||
351 | }).sortable("enable"); |
||
352 | } |
||
353 | dataManager.flowPanel(); |
||
354 | } |
||
355 | }); |
||
356 | if (dataManager.element.find('.categoryTree').length > 0) { |
||
357 | dataManager.element.find('.categoryTree').html('<img class ="img-responsive" src = "' + inji.options.appRoot + 'static/moduleAsset/Ui/images/ajax-loader.gif" />'); |
||
358 | inji.Server.request({ |
||
359 | url: 'ui/dataManager/loadCategorys', |
||
360 | data: {params: params, modelName: this.modelName, managerName: this.managerName}, |
||
361 | success: function (data) { |
||
362 | dataManager.element.find('.categoryTree').html(data); |
||
363 | dataManager.element.find('.categoryTree [data-path="' + instance.categoryPath + '"]').parent().addClass('active'); |
||
364 | dataManager.element.find('.treeview').treeview(); |
||
365 | $(instance.element).find('.categoryTree').sortable().sortable("disable"); |
||
366 | if (dataManager.mode == 'sort') { |
||
367 | $(instance.element).find('.categoryTree ul a[data-path]').map(function () { |
||
368 | this.onclick = null |
||
369 | }); |
||
370 | $(instance.element).find('.categoryTree ul').sortable({ |
||
371 | stop: function (event, ui) { |
||
372 | ids = $(instance.element).find('li'); |
||
373 | console.log(instance.element, ids) |
||
374 | i = 0; |
||
375 | while (ids[i]) { |
||
376 | var key = $(ids[i]).find('>a').data('id'); |
||
377 | var model = $(ids[i]).find('>a').data('model'); |
||
378 | console.log(key, model) |
||
379 | if (key && model) { |
||
380 | inji.Server.request({ |
||
381 | url: 'ui/dataManager/updateRow', |
||
382 | data: {params: instance.params, modelName: model, key: key, col: 'weight', col_value: i, managerName: instance.managerName, silence: true}, |
||
383 | }); |
||
384 | } |
||
385 | i++; |
||
386 | } |
||
387 | } |
||
388 | }).sortable("enable"); |
||
389 | } |
||
390 | } |
||
391 | }); |
||
392 | } |
||
393 | } |
||
394 | DataManager.prototype.switchCategory = function (categoryBtn) { |
||
496 | }); |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: